package cn.uniquesoft.api;

import cn.uniquesoft.service.IArticleService;
import cn.uniquesoft.service.IMyLikeService;
import cn.uniquesoft.service.IVideosService;
import cn.uniquesoft.vo.AnswerVO;
import cn.uniquesoft.vo.MyLikeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/api/like")
public class ApiLikeController {
    @Autowired
    private IMyLikeService myLikeService;
    @Value("${resource.pagesize}")
    private int pagesize;
    @Autowired
    private IArticleService articleService;
    @Autowired
    private IVideosService videosService;
    @RequestMapping(value = "/likeOrNot")
    @ResponseBody
    public AnswerVO toSaveVisit(int ifileid, int iuserid,int itype) {
        AnswerVO answerVO = new AnswerVO();
        MyLikeVO myLikeVO = new MyLikeVO();
        myLikeVO.setIfileid(ifileid);
        myLikeVO.setIuserid(iuserid);
        myLikeVO.setItype(itype);
        List<MyLikeVO> mlist  =  this.myLikeService.findAll(myLikeVO);
        int len = mlist.size();
        if(len >0){
            answerVO.setSuccess(true);
        }else{
            answerVO.setSuccess(false);
        }

        return answerVO;
    }

    @RequestMapping(value = "/likebtn")
    @ResponseBody
    public AnswerVO likebtn(int ifileid, int iuserid,int itype) {
        AnswerVO answerVO = new AnswerVO();
        MyLikeVO myLikeVO = new MyLikeVO();
        myLikeVO.setIfileid(ifileid);
        myLikeVO.setIuserid(iuserid);
        myLikeVO.setItype(itype);
        List<MyLikeVO> mlist  =  this.myLikeService.findAll(myLikeVO);
        if(mlist.size() == 0){
            myLikeVO.setDcreator(new Date());
            this.myLikeService.save(myLikeVO);
            answerVO.setMsg("点赞成功");
            answerVO.setSuccess(true);
        }else{
            this.myLikeService.removeById(mlist.get(0).getIid());
            answerVO.setMsg("取消点赞成功");
            answerVO.setSuccess(true);
        }

        return answerVO;
    }



    @RequestMapping(value = "/getMyLike")
    @ResponseBody
    public  List<MyLikeVO>  getMyLike(int iid, int iuserid, String option) {

        List<MyLikeVO> itemList = new ArrayList<>();
        MyLikeVO item = new MyLikeVO();
        item.setIid(iid);
        item.setIuserid(iuserid);
        item.setPageSize(this.pagesize);
        if(option.equals("down")){
            if(iid==0){
                itemList =   this.myLikeService.getItemsFirst(item);
            }else{
                itemList =   this.myLikeService.findListDown(item);
            }

        }else {
            itemList = this.myLikeService.findListUp(item);
        }
        for(MyLikeVO m : itemList){
            if(m.getItype()==1){
                m.setCname(this.articleService.findOneById(m.getIfileid()).getCtitle());
            }else{
                m.setCname(this.videosService.findOneById(m.getIfileid()).getCtitle());
            }
        }
        return itemList;

    }
}
